<?php
/**
 * Created by PhpStorm.
 * User: xin6841414
 * Date: 2018/6/13
 * Time: 15:24
 */

namespace  App\Modules\Manage\Http\Controllers;
use App\Http\Controllers\ManageController;
use App\Modules\Manage\Model\InformationModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Theme;

class InformationController extends ManageController
{

    public function __construct()
    {
        parent::__construct();
        $this->initTheme('manage');
        $this->theme->setTitle('信息管理');
    }

    public function getList()
    {
        $list = InformationModel::paginate(10);
        $view = [
            'list' =>$list,
            'search' => []
        ];
        return $this->theme->scope('manage.information', $view)->render();
    }

    public function handleInformation($id, $action)
    {
        switch ($action){
            case 'enable':
                $status = 1;
                break;
            case 'disable':
                $status = 0;
                break;
            default:
                $status = 1;
        }
        $status = InformationModel::where('id', $id)->update(['is_show' => $status]);
        if ($status) {
            return redirect()->back()->with(array('message' => '操作成功'));
        }
        return redirect()->back()->with(array('message' => '操作失败'));
    }

    public function getDeleteInformation($id){
        $re = InformationModel::destroy($id);
        if($re){
            return redirect()->back()->with(array('message' => '操作成功'));
        }
        return redirect()->back()->with(array('message' => '操作失败'));
    }

    public function getEdit($id){
        $list = InformationModel::where('id',$id)->first();

        $data = array(
            'list'=>$list
        );
        return $this->theme->scope('manage.editInformation',$data)->render();
    }

    public function postAdd(Request $request)
    {
        $data = $request->except('_token');
        $file = $request->file('pic');
        $pic = '';
        if ($file) {

            $result = \FileClass::uploadFile($file, 'sys');
            $result = json_decode($result, true);
            $pic = $result['data']['url'];
        }
        $link =array(
            'title'=>$data['title'],
            'key'=>$data['key'],
            'content'=>isset($data['content'])? $data['content']: '',
            'pic' => $pic,
            'type' => $data['type'],
            'is_show'=>$data['is_show'],
            'sort'=>$data['sort'],
        );
        $rs = InformationModel::create($link);
        Cache::forget('info_'.$data['key']);
        if($rs){
            return redirect()->back()->with(array('message' => '操作成功'));
        }else{
            return redirect()->back()->with(array('message' => '操作失败'));
        }
    }

    public function postUpdateInformation(Request $request,$id){
        $data = $request->except('_token');
        $link = InformationModel::where('id',$id)->first();
        if(!$link){
            return redirect()->back()->with('error', '参数错误!');
        }
        $file = $request->file('pic');
        if($file)
        {
            $result = \FileClass::uploadFile($file, 'sys');
            $result = json_decode($result, true);
            $pic = $result['data']['url'];
        }
        else
        {
            $pic = $link['pic'];
        }
        $new_link = array(
            'title'=> $data['title'],
            'type'=> $data['type'],
            'pic' => $pic,
            'key'=> $data['key'],
            'content'=> isset($data['content'])? $data['content']: '',
            'is_show' => intval($data['is_show']),
            'sort'=> intval($data['sort'])
        );
        $rs = $link->update($new_link);
        Cache::forget('info_'.$data['key']);

        if($rs){
            return redirect('manage/information')->with(array('message' => '操作成功'));
        }
        else{
            return redirect()->back()->with(array('message' => '操作失败'));
        }
    }
}